/*
 * Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.
 */
package com.xinpan.exercise;

public class RemoveElement {
    public int removeElement(int[] A, int elem) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(A.length == 0)
            return 0;
        
        
        int l = A.length-1;
        int s = 0;
        while(s <= l)
        {
            if(A[s] == elem)
            {
                while(l >= 0 && A[l] == elem)
                    l--;
                if(l < 0 || l < s)
                    return s;
                    
                A[s] = A[l];
                s++;
                l--;
            }
            else
                s++;
        }
        return s;
    }
    
    public static void main(String[] args)
    {
    	RemoveElement re = new RemoveElement();
    	int[] A = {4,5};
    	re.removeElement(A, 4);
    }
}
